package com.song.hbasechap_7_dll;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ha.HAAdmin;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;

public class Chap7DLL {
	
//	static String[] tables = {"music_user_bhvr_attr","music_user_bhvr_attr_index"
//						,"music_user_bhvr_attr_nreg","music_user_bhvr_attr_nreg_index",
//						"music_tags"};
	static Map<String,String[]> tables = new HashMap<String, String[]>(){
		{
			put("music_user_bhvr_attr", new String[]{"bhvr","attr","ecf1","ecf2"});
			put("music_user_bhvr_attr_index", new String[]{"cf1","cf2"});
			put("music_user_bhvr_attr_nreg", new String[]{"bhvr","attr","ecf1","ecf2"});
			put("music_user_bhvr_attr_nreg_index", new String[]{"ecf1","ecf2"});
			put("music_tags", new String[]{"tags","cf1"});
		}
		};
	
	public static void  createTable(String tablename)
	{
		
		Configuration conf = HBaseConfiguration.create();
		try {
			HBaseAdmin admin = new HBaseAdmin(conf);
			HTableDescriptor tabdesc = new HTableDescriptor(tablename);
			if(!admin.tableExists(Bytes.toBytes(tablename)))
			{
				String[] cols = tables.get(tablename);
				for(String col :cols)
				{
					HColumnDescriptor colDesc = new HColumnDescriptor(Bytes.toBytes(col));
					tabdesc.addFamily(colDesc);
					
				}
				admin.createTable(tabdesc);
			}else{
				System.out.println("table "+tablename +" is Exists ！");
			}
			
			
//			admin.disableTable(tableName);
//			admin.deleteTable(tableName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 *   修改列
	 * @param table
	 * @param oldcol
	 * @param newcol
	 */
	public static void TestModifyCol(String table ,String oldcol , String newcol)
	{
		Configuration conf = HBaseConfiguration.create();
		try {
			HBaseAdmin admin =  new HBaseAdmin(conf);
			HColumnDescriptor coldes = new HColumnDescriptor(Bytes.toBytes("col2"));
			admin.addColumn(Bytes.toBytes(table), coldes);
//			HColumnDescriptor mcols = new 
//			admin.modifyColumn();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	public static void createTable()
	{
		for(String tab :tables.keySet())
		{
			createTable(tab);
		}
	}
	
	
	public static void main(String[] args) {
		
		
		
		
		
	}

}
